<template>
  <div class="container">
    <el-dialog
      :visible.sync="changSbjShow"
      v-bind="$attrs"
      v-on="$listeners"
      @close="close"
      :title="changeState"
    >
      <el-form
        ref="sbjForm"
        :model="roleForm"
        :rules="rules"
        size="medium"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            v-model="roleForm.subjectName"
            placeholder="请输入学科名称"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay" required>
          <el-switch v-model="roleForm.isFrontDisplay"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from "../../api/hmmm/subjects";
export default {
  inheritAttrs: false,
  components: {},
  props: {
    changeState: {
      type: String,
    },
    changSbjShow: {
      type: Boolean,
      default: false,
    },
    changeSbjId: {
      type: Number,
    },
  },
  data() {
    return {
      roleForm: {
        subjectName: undefined,
        isFrontDisplay: false,
      },
      rules: {
        subjectName: [
          {
            required: true,
            message: "请输入学科名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs["sbjForm"].resetFields();
    },
    close() {
      this.$emit("update:changSbjShow", false);
    },
    async handelConfirm() {
      if (this.changeState === "新增学科") {
        await add(this.roleForm);
        await this.$parent.upDateList();

        this.$refs["sbjForm"].validate((valid) => {
          if (!valid) return;
          this.close();
        });
        this.roleForm.subjectName = "";
      } else if (this.changeState === "修改学科") {
        update({
          id: this.changeSbjId,
          ...this.roleForm,
        });
        await this.$parent.upDateList();

        this.$refs["sbjForm"].validate((valid) => {
          if (!valid) return;
          this.close();
        });
        this.roleForm.subjectName = "";
      }
    },
  },
};
</script>

<style scoped lang='less'></style>
